<?php
namespace Index\Controller;
//use Think\Controller;
use Index\Controller\CommonController;
class ProfileController extends CommonController{

    /**
     * 个人中心资料
     */
    public function indexAction(){

        $id = $_SESSION['uid'];

        $model = M();

        $sql = "select * from user where id='{$id}'";

        $userInfo =  $model->query($sql);
        $userInfo =  $userInfo[0];

        $userInfo['birthday']=$this->_changeDate($userInfo['birthday']);

        $sql = "select * from jobs";

        $jobsInfo =  $model->query($sql);

        $this->assign('userInfo',$userInfo);
        $this->assign('jobsInfo',$jobsInfo);

        $this->display('index');
    }

    public function updateAction(){
        if(IS_POST){
            $id = $_SESSION['uid'];
            $postData = I('post.');

            $birthday = $postData['birthday_y'].'-'.$postData['birthday_m'].'-'.$postData['birthday_d'];

            $sql1= "update user set turename='{$postData['turename']}',sex='{$postData['sex']}',birthday='{$birthday}',job='{$postData['job']}',city='{$postData['city']}',province='{$postData['province']}',introduce='{$postData['introduce']}',qq='{$postData['qq']}',phone='{$postData['phone']}',mail='{$postData['mail']}',site='{$postData['site']}' where id='{$id}' ";

            $modle = M();

            $modle->execute($sql1);

            $this->success('修改成功','index',2);

        }else{
            $this->error('非法请求','index',2);
        }
    }

    /***
     * 密码安全设置
     */
    public function securityAction(){
            $id = $_SESSION['uid'];
            
            if ($_POST) {
                $password = md5(I('post.old_password')); 
                // var_dump(I('post.password'));
                $password1 = md5(I('post.password')); 
                $password2 = md5(I('post.re_password')); 
                // var_dump($password);
                // var_dump($password1);
                // var_dump($password2);
                    if ($password==$_SESSION['password']) {
                                    
                                if ($password1==$password2) {

                                    $sql1= "update user set password='{$password2}' where id='{$id}' ";

                                    $modle = M();

                                    $modle->execute($sql1);

                                    $this->success('修改成功',U('Index/Login/index'),2);

                                    }else{
                                        $this->error('两次密码不一样','',2);
                                    }
                    }else{

                        $this->error('密码不正确','',2);
                      }
            }else{
                $this->display('security');
            }
    }


    /**
     * 把生日处理成年月日
     */
    private function _changeDate($date){

        $arr = explode('-',$date);
        $dateArr = array();
        $dateArr['birthday_y'] = $arr[0];
        $dateArr['birthday_m'] = $arr[1];
        $dateArr['birthday_d'] = $arr[2];
        return $dateArr;
    }

//上传图片到空间
    public function meichuanAction(){
       $id = $_SESSION['uid'];
       $save_path = './Uploads/avatars/'.date('Ymd',time() ); //定义一个要上传头像的目录
       //如果没有这么目录,那么就创建这个目录
       is_dir($save_path) || mkdir($save_path,0777,TRUE);
       if (is_uploaded_file($_FILES['upload_file']['tmp_name'])) {
           $uniqid = uniqid();
           $filename = $save_path . '/' . $uniqid . '.jpg';
           move_uploaded_file($_FILES['upload_file']['tmp_name'],$filename);
           if (is_file($filename)) {
                $filename = substr($filename, 1);
               $model = M();
               $sql = "update user set face_url='$filename' where id='{$id}' ";
               $model->execute($sql);
               $_SESSION['face_url']=$filename;
               echo '上传头像成功!';
               exit ();//退出
           }else{
                die ( '上传头像失败!' );
           }
       }else{
            echo "你的操作不合法";
       }
    }




}